package com.example.androidmpcharts.util;

import android.os.Handler;
import android.os.Message;

import com.example.androidmpcharts.LineChartDynamicActivity;

import java.lang.ref.WeakReference;


/**
 * 功能：自定义Handler，通过弱引用的方式防止内存泄漏
 */
public class DemoHandler extends Handler {
    public static final int MSG_START = 1; // handler消息，开始添加点
    WeakReference<LineChartDynamicActivity> mReference;

    public DemoHandler(LineChartDynamicActivity activity) {
        mReference = new WeakReference<>(activity);
    }


    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        LineChartDynamicActivity lineChartDynamicActivity = mReference.get();
        if (lineChartDynamicActivity == null) {
            return;
        }
        switch (msg.what) {
            case MSG_START:
                lineChartDynamicActivity.addLine1Data(lineChartDynamicActivity.getRandom(30f));
                lineChartDynamicActivity.addLine2Data(lineChartDynamicActivity.getRandom(20f));
                lineChartDynamicActivity.addLine3Data(lineChartDynamicActivity.getRandom(10f));
                lineChartDynamicActivity.sendStartAddEntry();
                break;
            default:
        }
    }
}
